package com.mms.entity.tag;

import com.mms.entity.dao.HibernateDao;
import com.mms.utils.SaveExcelUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * Created by dangz on 2017-5-23.
 */
public class ExportTag extends BodyTagSupport {
    private static final Logger logger = LoggerFactory.getLogger(ExportTag.class);
    private static final long serialVersionUID = -3007463448279167711L;
    private String sql;
    private String hql;
    private int rowPerPage = 500;
    private String sheetName;
    private String columns;
    private String fileName;

    public ExportTag() {
    }

    public String getSql() {
        return this.sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String getHql() {
        return this.hql;
    }

    public void setHql(String hql) {
        this.hql = hql;
    }

    public int getRowPerPage() {
        return this.rowPerPage;
    }

    public void setRowPerPage(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public String getSheetName() {
        return this.sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public String getColumns() {
        return this.columns;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }

    public String getFileName() {
        return this.fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int doStartTag() throws JspException {
        try {
            HttpServletResponse response = (HttpServletResponse)this.pageContext.getResponse();
            ApplicationContext appCtx = ContextLoader.getCurrentWebApplicationContext();
            HibernateDao hibernateDao = (HibernateDao)appCtx.getBean("hibernateDao");
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            if(StringUtils.isEmpty(this.fileName)) {
                this.fileName = String.valueOf(System.currentTimeMillis());
            } else {
                this.fileName = URLEncoder.encode(this.fileName, "UTF-8");
            }

            response.setHeader("Content-disposition", "attachment; filename=" + this.fileName + ".xls");
            OutputStream out = response.getOutputStream();
            if(StringUtils.isNotEmpty(this.sql)) {
                SaveExcelUtil.export(hibernateDao.getCurrentSession(), this.sql, this.sheetName, StringUtils.split(this.columns, ","), this.rowPerPage, out);
            } else {
                SaveExcelUtil.export(hibernateDao.getCurrentSession(), this.hql, this.sheetName, StringUtils.split(this.columns, ","), this.rowPerPage, out);
            }
        } catch (Exception var5) {
            logger.error("", var5);
        }

        return 0;
    }

    public int doEndTag() throws JspException {
        return 5;
    }
}
